package board;

import java.util.*;

import javax.servlet.http.*;

import model.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.*;

import util.*;

public class QnaBoardAction extends DispatchAction{


	/*****************
	 * list Action
	 *****************/

	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {
		
		QnaForm qnaForm = (QnaForm) form;
		QnaBoardDAO qnaboardDAO = new QnaBoardDAO();
 		
		//paging 
		Pager pager = new Pager();	
		Vector<QnaBoard> lists = null;
		
		//menu setting
		Menu menu = new Menu();
		menu = FreeBoardAction.setMainMenu(menu);
		request.getSession().setAttribute("menu",menu);
		
		//작성자, 내용에 따라 검색	
		String qnasearch = qnaForm.getQnasearch();
		String qnasearchfield = qnaForm.getQnasearchfield();
		String word=null;
		System.out.println(qnasearch +qnasearchfield);
		 if(qnasearch!=null){
			 if(qnasearchfield==null){
				word = (String)request.getSession().getAttribute("qnasearchfield"); 
			 } else{
				 word = qnasearchfield;
				 
				 request.getSession().setAttribute("qnasearchfield", word);

			 }
				System.out.println(qnasearch +word);
				System.out.println("====================");
			 if(qnasearch.equals("qbtitle")){
					
					System.out.println("제목 : "+ qnasearch + word);
					
					long totalsearch =qnaboardDAO.getTotalSearch(qnasearch,word );
					long totalRows = totalsearch;
					System.out.println(totalRows);
					pager.init(request, totalRows,10, 5);
					lists=qnaboardDAO.getSearch(pager.getCurrentPage(), pager.getRowsPerPage(), word, qnasearch);
									
			} else if(qnasearch.equals("mid")){
					System.out.println("id  : " + qnasearch +word);
					long totalsearch =qnaboardDAO.getTotalSearch(qnasearch,word);
					
					long totalRows = totalsearch;
					System.out.println(totalRows);
					pager.init(request,  totalRows,10, 5);
					lists=qnaboardDAO.getSearch(pager.getCurrentPage(), pager.getRowsPerPage(), word, qnasearch);
				
				}

		} else if(qnasearch==null||qnasearch.equals("")){
			
			long totalRows = qnaboardDAO.getTotalRows();
			pager.init(request,  totalRows,10, 5);
			lists=qnaboardDAO.getQnaBoardListPage(pager.getCurrentPage(), pager.getRowsPerPage());
			request.getSession().setAttribute("qnasearchfield",null);
		}
		 
		//DTO에서 뽑아온데이터를 Vector형으로 Form에 담기
		 Vector<QnaForm> qblist = new Vector<QnaForm>();
			
		for(QnaBoard qb : lists){
			QnaForm qbForm = new QnaForm();
			qbForm.setQbno(String.valueOf(qb.getQbno()));
			qbForm.setMid(qb.getMid());
			if(qb.getQbrefno()==qb.getQbno()){
				qbForm.setQbtitle(" [Q] " + qb.getQbtitle());
				
				System.out.println("qbno=qbrefno :"+qbForm.getQbtitle());
			}  else{
				
				qbForm.setQbtitle(qb.getQbtitle());
				System.out.println("qbno!=qbrefno:"+qbForm.getQbtitle());
			}

			if(qb.getQbtitle().length()>=35){

				qbForm.setQbtitle(qbForm.getQbtitle().substring(0,35)+".....");

			}else{

				qbForm.setQbtitle(qbForm.getQbtitle());
			}
			
	
			qbForm.setQbdate(qb.getQbdate());
			qbForm.setQbcontent(qb.getQbcontent());
			qbForm.setQbcounter(String.valueOf(qb.getQbcounter()));
			qbForm.setQbrefno(String.valueOf(qb.getQbrefno()));
			qbForm.setRownum(String.valueOf(qb.getRownum()));
			
			
			qblist.add(qbForm);
		}
		
		qnaForm.setQblist(qblist);;				
		User user = (User)request.getSession().getAttribute("user");
		String mcnick = user.getMid();		
		user.setMcnick(mcnick);
		request.getSession().setAttribute("user", user);
		request.getSession().setAttribute("pager", pager);	
		return mapping.findForward("qnalist");
		
	}

	/*********************
	 * insertForm Action
	 *********************/
	public ActionForward insertForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		QnaForm qnaForm = (QnaForm)form;
		User user = (User)request.getSession().getAttribute("user");
		String mcnick = user.getMid();		
		String mid = user.getMid();
		
		qnaForm.setMcnick(mcnick);
		qnaForm.setMid(mid);
				
		return  mapping.findForward("qnainsert");
	}

	/******************
	 * insert Action
	******************/
	public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		QnaForm qnaForm = (QnaForm)form;
		QnaBoardDAO qnaboardDAO = new QnaBoardDAO();
		QnaBoard qb = new QnaBoard();	
		
		if(qnaForm.getQbtitle().trim().length()<1) return mapping.findForward("qnainsert");
		if(qnaForm.getQbcontent().trim().length()<1) return mapping.findForward("qnainsert");

		qb.setMid(qnaForm.getMid());
		qb.setQbtitle(qnaForm.getQbtitle());
		qb.setQbcontent(qnaForm.getQbcontent());

		
		System.out.println(qnaForm.getQbno());
		System.out.println(qnaForm.getQbrefno());
		if(qnaForm.getQbrefno()!="") {
			qb.setQbrefno(Integer.parseInt(qnaForm.getQbrefno()));
			qb.setQbtitle(" └ [A] "+qnaForm.getQbtitle());

		} else{

			qb.setQbtitle(qnaForm.getQbtitle());
		}
	
			
		if(qnaboardDAO.insert(qb)){
		
			ActionForward af = new ActionForward();
			af.setPath("/qnaboard.do?method=list");
			af.setRedirect(true);
			return  af;
		} else{
		
			return mapping.findForward("qnainsert");
		}
	
	}

	/***********************
	 * updateForm Action*
	***********************/
	public ActionForward updateForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		QnaForm qnaForm = (QnaForm)form;
		User user = (User)request.getSession().getAttribute("user");
		String mid = user.getMid();
		
		qnaForm.setMid(mid);

		return  mapping.findForward("qnaupdate");
	}

	/******************
	 * update Action  *
	 ******************/
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		QnaForm qnaForm = (QnaForm)form;
		QnaBoardDAO qnaboardDAO =new QnaBoardDAO();
		QnaBoard qb = new QnaBoard();
		
		if(qnaForm.getQbtitle().trim().length()<1) return mapping.findForward("qnaupdate");
		if(qnaForm.getQbcontent().trim().length()<1) return mapping.findForward("qnaupdate");
		
		qb.setQbno(Integer.parseInt(qnaForm.getQbno()));
		qb.setQbtitle(qnaForm.getQbtitle());
		qb.setQbcontent(qnaForm.getQbcontent());

		Boolean a =qnaboardDAO.updateTitleContents(qb);

		
		ActionForward af = new ActionForward();
		af.setPath("/qnaboard.do?method=detailview&qbno="+ qb.getQbno());
		af.setRedirect(true);
		return  af;
	}


	/*********************
	 * detailview Action *
	 *********************/
	public ActionForward detailview(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		QnaForm qnaForm = (QnaForm)form;
		User user =(User)request.getSession().getAttribute("user");
		int qbno = Integer.parseInt(request.getParameter("qbno"));
		QnaBoardDAO qnaboardDAO = new QnaBoardDAO();
		QnaBoard qb = qnaboardDAO.getQnaBoard(qbno);

		System.out.println(user.getMid());
		
		
		if(qb!=null){
			if(!user.getMid().equals(qb.getMid())){
	
				qnaboardDAO.updateReadCount(qbno);
			}
			//DTO에서 뽑아 form에 정의
			qnaForm.setQbno(String.valueOf(qb.getQbno()));
			qnaForm.setMid(qb.getMid());
			qnaForm.setQbtitle(qb.getQbtitle());
			qnaForm.setQbdate(qb.getQbdate());
			qnaForm.setQbcontent(qb.getQbcontent());
			qnaForm.setQbcounter(String.valueOf(qb.getQbcounter()));
			qnaForm.setQbrefno(String.valueOf(qb.getQbrefno()));
			qnaForm.setRownum(String.valueOf(qb.getRownum()));
						
			qnaForm.setMcnick(qb.getMid());

			//next,preview
			int preview = qnaboardDAO.getPreview(qb);
			int next = qnaboardDAO.getNext(qb);
			
			System.out.println("이전글" +preview);
			System.out.println("클릭" +qb.getQbno());
			System.out.println("다음글" +next);
			System.out.println();			

			
			QnaForm pForm = new QnaForm();
			QnaForm nForm = new QnaForm();
			int num= qb.getQbno();
			
	//		setting preview and next articles

			if(next>num){
		
				qb =qnaboardDAO.getQnaBoard(next);
				System.out.println();
				nForm.setQbno(String.valueOf(qb.getQbno()));
				nForm.setMid(qb.getMid());
				nForm.setQbtitle(qb.getQbtitle());
				nForm.setQbdate(qb.getQbdate());
				nForm.setQbcontent(qb.getQbcontent());
				nForm.setQbcounter(String.valueOf(qb.getQbcounter()));
				nForm.setQbrefno(String.valueOf(qb.getQbrefno()));
				nForm.setRownum(String.valueOf(qb.getRownum()));
				qnaForm.setNext(nForm);	

			} else{
				qnaForm.setNext(null);
			}
			if(preview<num) {
				
				qb =qnaboardDAO.getQnaBoard(preview);
				pForm.setQbno(String.valueOf(qb.getQbno()));
				pForm.setMid(qb.getMid());
				pForm.setQbtitle(qb.getQbtitle());
				pForm.setQbdate(qb.getQbdate());
				pForm.setQbcontent(qb.getQbcontent());
				pForm.setQbcounter(String.valueOf(qb.getQbcounter()));
				pForm.setQbrefno(String.valueOf(qb.getQbrefno()));
				pForm.setRownum(String.valueOf(qb.getRownum()));
				
				qnaForm.setPreview(pForm);

			} else {
				qnaForm.setPreview(null);
			}
		
			return  mapping.findForward("qnaview");
		} else return  list(mapping,form,request,response);		
	}

	/******************
	 *  delete Action  *
	 ******************/
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		QnaForm qnaForm = (QnaForm)form;
		QnaBoardDAO qnaboardDAO = new QnaBoardDAO();
		QnaBoard qb = new QnaBoard();
		int qbno =Integer.parseInt(qnaForm.getQbno());
		
		qnaboardDAO.delete(qbno);

		return  list(mapping,form,request,response);
	}


}


